package cn.jvav.zh;

import cn.jvav.zh.game.map.object.MapObjects;
import cn.jvav.zh.io.ConsoleIO;
import cn.jvav.zh.game.Floor;
import cn.jvav.zh.game.Hero;
import cn.jvav.zh.game.logic.Coord;
import cn.jvav.zh.game.map.Floor0;
import cn.jvav.zh.game.map.Floor1;
import cn.jvav.zh.ui.Menu;
import cn.jvav.zh.ui.MenuItem;

import java.util.Arrays;


public class Game{
	public Hero hero = new Hero();
	public int floor = 1;
	public Floor[] floors = {
		Floor0.floor,
		Floor1.floor
	};
//	public void loadGames(){
//		try(Connection conn = JDBC.getConnection();
//			Statement stmt = conn.createStatement()){
//			stmt.executeQuery("select * from games");
//		}catch(SQLException e){
//			throw new RuntimeException(e);
//		}
//	}

	public static void main(String[] args){
		var game = new Game();
		var title = new Menu(
			Arrays.asList(
				new MenuItem("开始游戏", game::startGame),
				new MenuItem("载入游戏", Game::loadGame),
				new MenuItem("查看榜单", Game::showRank)
			)
		);
		title.choose();
	}

	public void startGame(){
		// 检测是否在真实控制台中运行
		if(System.console() == null){
			throw new RuntimeException("请在真实控制台游戏（cmd）");
		}
		while(true){
			draw();
			switch(ConsoleIO.getch()){
				case 27:
					// 菜单（暂时直接退出）
					return;
				case 224:
					switch(ConsoleIO.getch()){
						case 72:
							hero.up();
							break;
						case 75:
							hero.left();
							break;
						case 77:
							hero.right();
							break;
						case 80:
							hero.down();
							break;
					}
					//
			}
//			MapObjects mapObjects = floors[floor].map[hero.coord.y][hero.coord.x];

		}
	}

	public static void loadGame(){
		System.out.println("暂未开放");
	}

	public static void showRank(){
	}

	public void draw(){
		ConsoleIO.clearScreen();
		System.out.println("魔塔 第" + floor + "层");
		hero.showStatus();
		hero.showEquip();
		System.out.println("-".repeat(11 * 3 * 4));
		for(int i = 0; i < floors[floor].map.length; i++){
			for(int j = 0; j < floors[floor].map[i].length; j++){
				String name = floors[floor].map[i][j].name;
				if(hero.coord.equals(new Coord(j, i))){
					name = "勇者";
				}
				System.out.printf("%-" + (12 - name.length()) + "s", name);
			}
			System.out.println();
		}
		System.out.println("-".repeat(11 * 3 * 4));
	}
}
